package com.learn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.learn.base.ApiResult;
import com.learn.base.ApiResultUtils;
import com.learn.dto.req.*;
import com.learn.dto.resp.QueryProductDetailResp;
import com.learn.dto.resp.QueryProductListResp;
import com.learn.emuns.LearnResultEnums;
import com.learn.emuns.ProductStatusEnums;
import com.learn.entity.Product;
import com.learn.repository.ProductMapper;
import com.learn.service.ProductService;
import com.learn.utils.GlobalTokenUtil;
import com.learn.utils.JsonUtil;
import com.learn.validator.ProductValidate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper,Product> implements ProductService
{



	@Autowired
	private ProductMapper productMapper;
//
//	@Autowired
//	private ProductValidate productValidate;
//
//	@Override
//	public ApiResult<Boolean> saveAddProduct (AddProductReq req)
//	{
//		ApiResult<Boolean> validateResult = ProductValidate.validateAddProduct (req);
//		if (! validateResult.getData ())
//		{
//			return validateResult; // 校验失败，直接返回错误信息
//		}
//		Product product = new Product ();
//		product.setProductId (req.getProductId ());
//		product.setProductItemId (req.getProductItemId ());
//		product.setCode (req.getCode ());
//		product.setStartTime (req.getStartTime ());
//		product.setEndTime (req.getEndTime ());
//		product.setMembershipLevel (req.getMembershipLevel ());
//		product.setResourcePool (JsonUtil.to (req.getResourcePool ()));
//		product.setDescription (req.getDescription ());
//		product.setRenewalLimit (req.getRenewalLimit ());
//		product.setCreateTime (LocalDateTime.now ());
//		product.setOperatorId (GlobalTokenUtil.getUserAccount ());
//		product.setLastModifiedId (GlobalTokenUtil.getUserAccount ());
//
//		Integer addProductResult = productMapper.insert(product);
//		if (addProductResult <= 0)
//		{
//			return ApiResultUtils.build (LearnResultEnums.ADD_PRODUCT.getStatus (),
//					LearnResultEnums.ADD_PRODUCT.getErrMsg (), false);
//		}
//		return ApiResultUtils.build (LearnResultEnums.SUCCESS.getStatus (), LearnResultEnums.SUCCESS.getErrMsg (),
//				true);
//
//	}
//
	@Override
	public ApiResult<QueryProductListResp> saveQueryProductList (QueryProductListReq req)
	{

//		Page<Product> page = new Page<> (req.getPageIndex (), req.getPageNum ());
//		IPage<Product> productList = productMapper;
//		QueryProductListResp queryProductListResp = new QueryProductListResp ();
//		List<Product> records = productList.getRecords ();
//		List<Product> products = new ArrayList<> ();
//		for (int i = 0; i < records.size (); i++)
//		{
//			Product productDto = records.get (i);
//			Product product = new Product ();
//			product.setId (productDto.getId ());
//			product.setProductId (productDto.getProductId ());
//			product.setProductItemId (productDto.getProductItemId ());
//			product.setCode (productDto.getCode ());
//			product.setStatus (productDto.getStatus ());
//			product.setCreateTime (productDto.getCreateTime ());
//			product.setStartTime (productDto.getStartTime ());
//			product.setEndTime (productDto.getEndTime ());
//			product.setMembershipLevel (productDto.getMembershipLevel ());
//			product.setResourcePool (productDto.getResourcePool ());
//			product.setDescription (productDto.getDescription ());
//			product.setRenewalLimit (productDto.getRenewalLimit ());
//			product.setOperatorId (productDto.getOperatorId ());
//			product.setLastModifiedId (productDto.getLastModifiedId ());
//			products.add (product);
//		}
//		queryProductListResp.setTotal (productList.getTotal ());
//		queryProductListResp.setPageIndex (req.getPageIndex ());
//		queryProductListResp.setPageNum (req.getPageNum ());
//		queryProductListResp.setRecords (products);
//		return ApiResultUtils.build (LearnResultEnums.SUCCESS.getStatus (), LearnResultEnums.SUCCESS.getErrMsg (),
//				queryProductListResp);

		Page<Product> page = new Page<> (req.getPageIndex (), req.getPageNum ());
		QueryWrapper<Product> productQueryWrapper=new QueryWrapper<> ();
		productQueryWrapper.lambda ()
				.eq (StringUtils.isNotEmpty (req.getStatus ()),Product::getStatus,req.getStatus ())
				.like (StringUtils.isNotEmpty (req.getName ()),Product::getName,req.getName ());

		Page<Product> page1 = productMapper.selectPage (page, productQueryWrapper);
		return null;
	}
//
//	@Override
//	public ApiResult<Boolean> saveDeleteProduct (DeleteProductReq req)
//	{
//		//mybatis-plus的应用
//		Product product = productMapper.selectById (req.getId ());
//		if (product == null)
//		{
//			return ApiResultUtils.build (LearnResultEnums.PRODUCT_NOT_FOUND.getStatus (), LearnResultEnums.PRODUCT_NOT_FOUND.getErrMsg (), null);
//		}
//		//检查是否已删除
//		if (ProductStatusEnums.DELETE.getType ().equals (product.getStatus ())){
//			return ApiResultUtils.fail ("产品已删除，无需重复操作");
//		}
//
//		//执行逻辑删除
//		product.setStatus (ProductStatusEnums.DELETE.getType ());
//		product.setLastModifiedId (GlobalTokenUtil.getUserAccount ());
//
//		Integer deleteProductResult = productMapper.deleteById (req.getId ());
//		if (deleteProductResult <= 0)
//		{
//			return ApiResultUtils.build (LearnResultEnums.DELETE_PRODUCT.getStatus (),
//					LearnResultEnums.DELETE_PRODUCT.getErrMsg (), false);
//		}
//		return ApiResultUtils.build (LearnResultEnums.SUCCESS.getStatus (), LearnResultEnums.SUCCESS.getErrMsg (),
//				true);
//	}
//
//	@Override
//	public ApiResult<QueryProductDetailResp> saveQueryProductDetail (QueryProductDetailReq req)
//	{
//
//		Product product = productMapper.selectById (req.getId ());
//		if (product == null)
//		{
//			return ApiResultUtils.build (LearnResultEnums.PRODUCT_NOT_FOUND.getStatus (),
//					LearnResultEnums.PRODUCT_NOT_FOUND.getErrMsg (), null);
//		}
//		QueryProductDetailResp queryProductDetailResp = new QueryProductDetailResp ();
//		queryProductDetailResp.setId (product.getId ());
//		queryProductDetailResp.setProductId (product.getProductId ());
//		queryProductDetailResp.setProductItemId (product.getProductItemId ());
//		queryProductDetailResp.setCode (product.getCode ());
//		queryProductDetailResp.setStatus (product.getStatus ());
//		queryProductDetailResp.setCreateTime (product.getCreateTime ());
//		queryProductDetailResp.setStartTime (product.getStartTime ());
//		queryProductDetailResp.setEndTime (product.getEndTime ());
//		queryProductDetailResp.setMembershipLevel (product.getMembershipLevel ());
//		queryProductDetailResp.setResourcePool (product.getResourcePool ());
//		queryProductDetailResp.setDescription (product.getDescription ());
//		queryProductDetailResp.setRenewalLimit (product.getRenewalLimit ());
//		queryProductDetailResp.setOperatorId (product.getOperatorId ());
//		queryProductDetailResp.setLastModifiedId (product.getLastModifiedId ());
//		return ApiResultUtils.buildSuccess (queryProductDetailResp);
//	}
//
//	@Override
//	public ApiResult<Boolean> saveUpdateProduct (UpdateProductReq req)
//	{
//		ApiResult<Boolean> validateResult = ProductValidate.validateUpdateProduct (req);
//		if (! validateResult.getData ())
//		{
//			return validateResult;
//		}
//		//禁止通过更新接口直接删除
//		if (ProductStatusEnums.DELETE.getType ().equals (req.getStatus ())){
//			return ApiResultUtils.fail ("请调用专用删除接口进行逻辑删除");
//		}
//
//		//查询数据库中当前产品的状态
//		Product product = productMapper.selectById (req.getId ());
//		if (product == null){
//			return ApiResultUtils.build (LearnResultEnums.PRODUCT_NOT_FOUND.getStatus (),LearnResultEnums.PRODUCT_NOT_FOUND.getErrMsg (),null);
//		}
//
//
//			product.setProductId (req.getProductId ());
//			product.setProductItemId (req.getProductItemId ());
//			product.setStartTime (req.getStartTime ());
//			product.setEndTime (req.getEndTime ());
//			product.setResourcePool (JsonUtil.to (req.getResourcePool ()));
//			product.setDescription (req.getDescription ());
//			product.setRenewalLimit (req.getRenewalLimit ());
//			product.setStatus (req.getStatus ());
//
//
//		product.setOperatorId (GlobalTokenUtil.getUserAccount ());
//		product.setLastModifiedId (GlobalTokenUtil.getUserAccount ());
//		product.setCreateTime (LocalDateTime.now ());
//		Integer updateProductResult = productMapper.updateById (product);
//		if (updateProductResult <= 0)
//		{
//			return ApiResultUtils.build (LearnResultEnums.UPDATE_PRODUCT.getStatus (),
//					LearnResultEnums.UPDATE_PRODUCT.getErrMsg (), false);
//		}
//		return ApiResultUtils.build (LearnResultEnums.SUCCESS.getStatus (), LearnResultEnums.SUCCESS.getErrMsg (),
//				true);
//
//	}

}
